<template>
    <div>
        <el-dialog
            title="新增康复记录"
            :visible.sync="dialogVisible"
            @closed="close"
            @open="open"
            :close-on-click-modal="false"
            :append-to-body="true"
            width="600px">
            <el-form :model="formdata" label-width="120px" ref="addform" :rules="rules">
                <el-form-item label="学生：" prop="bSID" v-if="!bSID">
                    <el-select v-model="formdata.bSID" placeholder="请选择" clearable filterable :filter-method="filterMethod">
                        <el-option
                        v-for="item in options"
                        :key="item.SID"
                        :label="item.SName"
                        :value="item.SID">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="上课时间：" prop="bClassTime">
                    <el-date-picker
                        v-model="formdata.bClassTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="康复中心：" prop="bRehabilitation">
                    <el-input
                    v-model.trim="formdata.bRehabilitation"
                    placeholder="请输入康复中心"
                    maxlength="30"
                    show-word-limit
                    ></el-input>
                </el-form-item>
                <el-form-item label="康复内容：" prop="bRehabilitationContent">
                    <el-input v-model.trim="formdata.bRehabilitationContent" placeholder="请输入康复内容" maxlength="100"
                    show-word-limit></el-input>
                </el-form-item>
                <el-form-item label="课时：" prop="bDuration">
                    <el-input-number v-model="formdata.bDuration" :min="1" :max="1000" label="请输入课时"></el-input-number>
                    <!-- <el-input v-model.trim="formdata.bDuration" placeholder="请输入课时"></el-input> -->
                </el-form-item>
                <el-form-item label="康复教师：" prop="bRehabilitationTeach">
                    <el-input v-model.trim="formdata.bRehabilitationTeach" placeholder="请输入"></el-input>
                    <!-- <el-select v-model="formdata.bRehabilitationTeach" placeholder="请选择">
                        <el-option
                            v-for="item in teacherlist"
                            :key="item.id"
                            :label="item.label"
                            :value="item.id">
                        </el-option>
                    </el-select> -->
                </el-form-item>
                <!-- <el-form-item label="备注：" prop="remark">
                    <el-input
                    type="textarea"
                    :autosize="{ minRows: 2, maxRows: 4}"
                    placeholder="请输入备注"
                    v-model="formdata.remark">
                    </el-input>
                </el-form-item> -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import {addStudyRehabilitation,editStudyRehabilitation} from '@/api/index.js'
// 添加康复记录
export default {
    data() {
        const bRehabilitation = this.$util.getUserInfo().deptname;
        return {
            dialogVisible:false,
            teacherlist:[],
            options:[],
            formdata:{
                bSID:'',
                bClassTime:'',
                bRehabilitation:bRehabilitation,
                bRehabilitationTeach:'',
                bDuration:'',
                bRehabilitationContent:''
            },
            rules: {
                bSID:[
                    { required: true, message: '学生不能为空', trigger: 'change' }
                ],
                bClassTime: [
                    { required: true, message: '时间不能为空', trigger: 'change' }
                ],
                bRehabilitation: [
                    { required: true, message: '康复中心不能为空', trigger: 'blur' }
                ]
            },
        }
    },
    props:{
        bSID:{
            type:String,
            default:''
        },
        studylist:{
            type:Array,
            default:function() {
                return []
            }
        },
        eidtinfo:{
            type:null
        }
    },
    watch:{
        studylist:{
            immediate:true,
            handler:function(val) {
                this.options = val;
            }
        }
    },
    created(){
        // 获取教师列表
        this.getTeacherLists()
    },
    methods:{
        getTeacherLists(){
            
        },
        open() {
            this.$nextTick(() => {
                if(this.eidtinfo) {
                    this.formdata.bSID = this.eidtinfo.SID;
                    this.formdata.bClassTime = this.eidtinfo.ClassTime;
                    this.formdata.bRehabilitation = this.eidtinfo.Rehabilitation;
                    this.formdata.bRehabilitationTeach = this.eidtinfo.RehabilitationTeach;
                    this.formdata.bDuration = this.eidtinfo.Duration;
                    this.formdata.bRehabilitationContent = this.eidtinfo.RehabilitationContent;
                }
            })
        }, 
        filterMethod(query){
            console.log(query)
            if (query !== '') {
                this.loading = false;
                this.options = this.studylist.filter(item => {
                return item.SName.toLowerCase()
                    .indexOf(query.toLowerCase()) > -1;
                });
            } else {
                this.options = this.studylist;
            }
        },
        save(){
            this.$refs.addform.validate((valid) => {
                if (valid) {
                    if(this.eidtinfo) {
                        editStudyRehabilitation({...this.formdata,bSRID:this.eidtinfo.SRID}).then((res) => {
                            if(res) {
                                if(res.code == 200) {
                                    this.$util.tipsMsg('success','修改成功！');
                                    this.dialogVisible = false;
                                    this.$emit('addsuccess',true);
                                } else {
                                    this.$util.tipsMsg('error',res.message);
                                }
                            }
                        }).catch((err) => {

                        })
                    } else {
                        let params = {
                            ...this.formdata
                        }
                        if(this.bSID) {
                            params.bSID = this.bSID
                        }
                        addStudyRehabilitation({...params}).then((res) => {
                            if(res) {
                                if(res.code == 200) {
                                    this.$util.tipsMsg('success','添加成功！');
                                    this.dialogVisible = false;
                                    this.$emit('addsuccess',true);
                                } else {
                                    this.$util.tipsMsg('error',res.message);
                                }
                            }
                        }).catch((err) => {

                        })
                    }
                } else {
                    return false;
                }
            })
        },
        close(){
            this.$refs.addform.resetFields();
        }
    }
}
</script>
<style lang="less" scoped>

</style>
